package com.qhw.test2;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AppCore {
    public static void main(String[] args) throws Exception{
        Class<?> A = Class.forName("com.qhw.test2.CoreService");
        Object c = A.newInstance();
        Method say = A.getDeclaredMethod("say");
        Field[] declaredFields = A.getDeclaredFields();
        //方法一：
        for(Field field : declaredFields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            Object u = type.newInstance();
            field.set(c, u);
        }
        //方法二：
//        for (Field field :declaredFields){
//            field.setAccessible(true);
//            if (field.getType().toString().endsWith("CustomerService")){
//                Class<?> B = Class.forName("com.qhw.test2.CustomerService");
//                field.set(c,B.newInstance() );
//            }
//            if (field.getType().toString().endsWith("OrderService")){
//                Class<?> C = Class.forName("com.qhw.test2.OrderService");
//                field.set(c,C.newInstance() );
//            }
//            if (field.getType().toString().endsWith("UserService")){
//                Class<?> D = Class.forName("com.qhw.test2.UserService");
//                field.set(c,D.newInstance() );
//            }
//        }
        //调用实例c的方法
        say.invoke(c);
    }
}
